package efr.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import efr.dao.PoezieDao;
import efr.dao.impl.PoezieDaoImpl;
import efr.entity.Poezie;

/**
 * Servlet implementation class ListPoeziiServlet
 */
public class ListPoeziiServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ListPoeziiServlet.class);
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListPoeziiServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		logger.debug("start doGet()");
		PoezieDao poezieDao = new PoezieDaoImpl();
		List<Poezie> listPoezii = poezieDao.getAllPoezii();
//		String prefix =  getServletContext().getRealPath("\\");
//		String contextNicu = getServletContext().getContextPath();
//		request.setAttribute("prefix", prefix);
//		request.setAttribute("contextNicu", contextNicu);
		request.setAttribute("listPoezii", listPoezii);
//		request.setAttribute("separator", File.separator);
//		request.setAttribute("pathSeparator", File.pathSeparator);
//		request.setAttribute("root", File.listRoots());
//		File currentDirectory = new File(new File(".").getAbsolutePath());
//		File currentDirectory = new File(new File("/usr/share/tomcat5/webapps/ROOT/").getAbsolutePath());
//		System.out.println(currentDirectory.getCanonicalPath());
//		System.out.println(currentDirectory.getAbsolutePath());
//		request.setAttribute("fileList", currentDirectory.list());
//		request.setAttribute("getCanonicalPath", currentDirectory.getCanonicalPath());
//		request.setAttribute("getAbsolutePath", currentDirectory.getAbsolutePath());
//		request.setAttribute("allFile", listDirectory("."));
		RequestDispatcher view = request.getRequestDispatcher("/indexPoezie.jsp");
		logger.debug("end doGet()");
		view.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
    // Files and directories statistics
    private static int allElementsCount = 0;
    private static int directoriesCount = 0;
    private static ArrayList<String> fileList = new ArrayList<String>();
    
	 public static ArrayList listDirectory(String directoryPath) {
//	    	ArrayList<String> fileList = new ArrayList<String>();
	    	// List all the directory contents
	        for (String contents: new File(directoryPath).list()) {
	             
	            allElementsCount++;
	             
	            // Directory path for files and directories
	            contents = directoryPath+"/"+contents;
	             
	            // Display full path names
//	            System.out.println(contents);
	            fileList.add(contents);
	             
	            // For directories go recursively down
	            if (new File(contents).isDirectory()) {
	                 
	                directoriesCount++;
	                 
	                // Go down to next directory
	                listDirectory(contents);
	            }
	        }
	        return fileList;
	    }
}
